package cn.sunlelin.educationserver.controller;

import cn.sunlelin.educationserver.entity.Announce;
import cn.sunlelin.educationserver.mapper.AnnounceMapper;
import cn.sunlelin.educationserver.service.AnnounceService;
import cn.sunlelin.educationserver.service.CourseService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class AnnounceController {
    @Autowired
    private AnnounceService announceService;

    @Autowired
    private CourseService courseService;

    @PostMapping("/user/announce/createAnnounce")
    public Map<String,Object> createAnnounce(@RequestBody Announce announce,
                                             @RequestHeader("userId") Integer userId){
        Map<String,Object> map=new HashMap<>();
        Map<String,Object> tCourse=courseService.selectCourseById(announce.getCourseId());
        if(!tCourse.get("userId").equals(userId)){
            map.put("code",500);
            map.put("msg","添加公告失败，你没有该权限");
            map.put("dataobject",null);
        }else {
            if (announceService.createAnnounce(announce)) {
                map.put("code", 200);
                map.put("msg", "添加公告成功");
                map.put("dataobject", null);
            } else {
                map.put("code", 500);
                map.put("msg", "添加公告失败");
                map.put("dataobject", null);
            }
        }
        return map;
    }

    @GetMapping("/announce/selectAnnounceById/{announceId}")
    public Map<String,Object> selectAnnounceById(@PathVariable Integer announceId){
        Map<String,Object> map=new HashMap<>();
        Map<String,Object> announce=announceService.selectAnnounceById(announceId);
        if(announce!=null){
            map.put("code",200);
            map.put("msg","Id查询公告成功");
            map.put("dataobject",announce);
        }
        else {
            map.put("code",500);
            map.put("msg","Id查询公告失败");
            map.put("dataobject",null);
        }
        return map;
    }

    @GetMapping("/announce/getPageByCourseId/{currentPage}/{pageSize}/{courseId}")
    public Map<String,Object> getPageByCourseId(@PathVariable Integer currentPage,
                                                @PathVariable Integer pageSize,
                                                @PathVariable Integer courseId){
        if(currentPage==null) currentPage=1;
        if(pageSize==null)pageSize=2;
        Page<Map<String,Object>> page= announceService.getPageByCourseId(currentPage,pageSize,courseId);
        List<Map<String,Object>> pageList=page.getResult();
        long total = page.getTotal();
        Map<String,Object> m=new HashMap<>();
        m.put("pageList",pageList);
        m.put("total",total);
        Map<String,Object> map=new HashMap<>();
        map.put("code",200);
        map.put("msg","课程Id分页查询公告成功");
        map.put("dataobject",m);
        return map;
    }

    @PostMapping("/user/announce/updateAnnounce")
    public Map<String,Object> updateAnnounce(@RequestBody Announce announce,
                                             @RequestHeader("userId") Integer userId){
        Map<String,Object> map=new HashMap<>();
        Map<String,Object> tAnnounce=announceService.selectAnnounceById(announce.getAnnounceId());
        if(!tAnnounce.get("userId").equals(userId)){
            map.put("code",500);
            map.put("msg","修改公告失败，你没有该权限");
            map.put("dataobject",null);
        }
        else {
            if (announceService.updateAnnounce(announce)) {
                map.put("code", 200);
                map.put("msg", "修改公告成功");
                map.put("dataobject", null);
            } else {
                map.put("code", 500);
                map.put("msg", "修改公告失败");
                map.put("dataobject", null);
            }
        }
        return map;
    }

    @GetMapping("/user/announce/deleteAnnounce/{announceId}")
    public Map<String,Object> deleteAnnounce(@PathVariable Integer announceId,
                                             @RequestHeader("userId") Integer userId){
        Map<String,Object> map=new HashMap<>();
        Map<String,Object> tAnnounce=announceService.selectAnnounceById(announceId);
        if(!tAnnounce.get("userId").equals(userId)){
            map.put("code",500);
            map.put("msg","删除公告失败，你没有该权限");
            map.put("dataobject",null);
        }
        else {
            if (announceService.deleteAnnounce(announceId)) {
                map.put("code", 200);
                map.put("msg", "删除公告成功");
                map.put("dataobject", null);
            } else {
                map.put("code", 500);
                map.put("msg", "删除公告失败");
                map.put("dataobject", null);
            }
        }
        return map;
    }
}
